﻿@{
    Layout="_Layout1.cshtml";

    string data = Request["data"];
    string hour = Request["h"];
    string minute = Request["m"];
    string doctorID = Request["ID"];
    string cancelCode = "";
    var db = Database.Open("Clinic");

    bool registered = false;
    if (IsPost)
    {
        registered = true;

        var name = Request["name1"];
        var surname = Request["surname1"];
        var pesel = Request["pesel1"];

        

        var checkPatient = "SELECT id FROM Patient WHERE Pesel = @0";
        var patientID = db.QuerySingle(checkPatient, pesel);

        if (patientID == null)  //TODO CHECK IF PATIENT EXISTS
        {
            var insertPatient = "INSERT INTO Patient (Name, Surname, Pesel) VALUES (@0, @1, @2)";
            db.Execute(insertPatient, name, surname, pesel);
            patientID = db.QuerySingle(checkPatient, pesel);
        }
        
        string termin = data + " " + hour + ":" + minute;
        cancelCode = "1234";
        // TODO: Random calncelcode generator
        // TODO: Captcha!


        var checkAppointment = "SELECT * FROM Appointment WHERE Doctor = @0 AND Termin = @1";
        var checkAppointmentResult = db.QuerySingle(checkAppointment,doctorID, termin);
        if (checkAppointmentResult == null)
        {
            var insertAppointment = "INSERT INTO Appointment (Doctor, Patient, Termin, CancelCode) VALUES (@0, @1, @2, @3)";
            db.Execute(insertAppointment, doctorID, patientID.id, termin, cancelCode);
        }
        else
        {
            cancelCode = "WTF W TYM TERMINIE JUZ KTOŚ JEST ZAREJESTROWANY";
        }
    }

    var singleQuery = "SELECT * FROM Doctor WHERE ID = @0";
    var doctor = db.QuerySingle(singleQuery, doctorID);





}

Chcesz sie zarejestrowac do: @doctor.Profession - @doctor.Name @doctor.Surname </br>
Dnia @data Na godzine @hour:@minute</br>
Wypełnij formularz poniżej a następnie kliknij przycisk "Zarejstruj", aby potwierdzić rejestrację.

<form name="register" method="post">
Imię</br>
    <input name="name1" type="text" value="" ></br>
Nazwisko</br>
    <input name="surname1" type="text" value=""></br>
PESEL</br>
    <input name="pesel1" type="text" value=""></br>
    
<input type="submit" value="Zarejestruj!">
</form>


@if(registered)
{
    <text>SUCCESFULL! Udało Ci się zarejestrować.</text>
    <text>Pamiętaj o kodzie do odwołania wizyty</text>
    <text>Twój kod to: @cancelCode</text>
} 